<?php
/**
 * Create by 11li
 * Date 2020/2/3
 * Time 11:25
 *
 */

namespace app\common\service;


use app\common\library\Auth;
use app\common\model\Danmu;
use app\common\model\Follow;
use app\common\model\Goods;
use app\common\model\Seller;
use app\common\model\User;
use app\common\model\Video;
use app\common\model\VideoLike;
use MongoDB\BSON\DbPointer;
use think\Db;
use think\Exception;

class VideoService extends BaseService
{
	
	public function __construct()
	{
		$this->model = new Video();
	}
	
	public static function getList($page, $goodsId = '')
	{
		$where = [
			'v.status' => StatusService::STATUS_NORMAL
		];
		
		$goodsId && $where['v.goods_id'] = $goodsId;
		
		$count = Db::name('video')->alias('v')
			->where($where)
			->count();
		
		$list = Db::name('video')->alias('v')
			->join('goods g', 'v.goods_id = g.id', 'left')
			->where($where)
			->page($page)
			->field('v.id, v.video_file, v.title, v.cover_image, v.view_number, v.like_number, from_unixtime(v.create_time, \'%Y-%m-%d\') as date, g.image as goods_image')
			->select();
		
		return [
			'total' => $count,
			'list' => $list,
			'page' => $page
		];
	}
	
	
	public function like($videoId, $userId)
	{
		if(!$videoId || !$userId) {
			throw new Exception('参数错误');
		}
		
		$model = new VideoLike();
		$row = VideoLike::get([
			'user_id' => $userId,
			'video_id' => $videoId,
			'type' => StatusService::VIDEO_TYPE_LIKE]);
		
		$status = true;
		$video = $this->model->find($videoId);
		$likeNumber = 1;
		if($row) {
			if($row['status'] == StatusService::STATUS_NORMAL) {
				$likeStatus = StatusService::STATUS_DISABLED;
				$likeNumber = -1;
				$status = false;
			} else {
				$likeStatus = StatusService::STATUS_NORMAL;
			}
			$res = $model->save([
				'status' => $likeStatus
			], ['id' => $row['id']]);
			
		} else {
			$res = $model->isUpdate(false)
				->save([
					'user_id' => $userId,
					'video_id' => $videoId,
					'type' => StatusService::VIDEO_TYPE_LIKE
				]);
		}
		
		$this->model->where(['id' => $videoId])
			->setInc('like_number', $likeNumber);
		
		
		return [
			'status' => $status,
			'number' => $video['like_number'] + $likeNumber
		];
	}
	
	public function getVideoById($id)
	{
		$row = $this->model
			->where(['id' => $id, 'status' => StatusService::STATUS_NORMAL])
			->find();
		if(!$row) {
			throw new Exception('记录不存在');
		}
		
		$row['user'] = User::get($row['user_id']);
		$row['goods'] = Goods::get($row['goods_id']);
		
		$follwModel = new Follow();
		$row['fans_number'] = $follwModel
			->where(['follow_user_id' => $row['seller']['user_id']])
			->count();
		
		$row['danmu_list'] = Db::name('danmu')
			->where(['video_id' => $id, 'status' => StatusService::STATUS_NORMAL])
			->field('content as text, time, color')
			->select();
		
		$likeWhere =['video_id' => $id, 'type' => StatusService::VIDEO_TYPE_LIKE, 'status' => StatusService::STATUS_NORMAL];
		$row['like_number'] = VideoLike::where($likeWhere)
			->count();
		
		$likeWhere['user_id'] = Auth::instance()->id;
		$like = VideoLike::where($likeWhere)->find();
		$row['is_like'] = $like ? true : false;
		
		$follow = $follwModel
			->where(['follow_user_id' => $row['seller']['user_id'], 'user_id' => Auth::instance()->id])
			->find();
		
		$row['is_follow'] = $follow ? true : false;
		
		return $row;
	}
	
	/**
	 * 添加弹幕
	 * @param $data
	 * @return false|int
	 */
	public function addDanmu($data)
	{
		return (new Danmu())->isUpdate(false)
			->save($data);
	}
	
	/**
	 * 创建视频
	 * @param $data
	 * @return false|int
	 * @throws Exception
	 * @throws \think\exception\DbException
	 */
	public function createVideo($data)
	{
		$goods = Goods::get(['id' => $data['goods_id'], 'status' => StatusService::STATUS_NORMAL]);
		
		if(!$goods) {
			throw new Exception('评价商品已下架');
		}
		
		$data['seller_id'] = $goods['seller_id'];
		
		$model = new Video();
		$res = $model->isUpdate(false)
			->allowField(true)
			->save($data);
		if($res) {
			return $model->getLastInsID();
		} else {
			throw new Exception('添加视频出错');
		}
	}
}